<!DOCTYPE html>
<html lang="{{ config.language }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ _('Settings') }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }
        .container {
            width: 80%;
            max-width: 800px;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"], input[type="number"], select {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 34px;
        }
        .switch input { 
            opacity: 0;
            width: 0;
            height: 0;
        }
        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            -webkit-transition: .4s;
            transition: .4s;
        }
        .slider:before {
            position: absolute;
            content: "";
            height: 26px;
            width: 26px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            -webkit-transition: .4s;
            transition: .4s;
        }
        input:checked + .slider {
            background-color: #2196F3;
        }
        input:focus + .slider {
            box-shadow: 0 0 1px #2196F3;
        }
        input:checked + .slider:before {
            -webkit-transform: translateX(26px);
            -ms-transform: translateX(26px);
            transform: translateX(26px);
        }
        .slider.round {
            border-radius: 34px;
        }
        .slider.round:before {
            border-radius: 50%;
        }
        button {
            padding: 10px 20px;
            background-color: #2196F3;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #0b7dda;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>{{ _('Settings') }}</h1>
        <form id="settingsForm">
            <div class="form-group">
                <label for="language">{{ _('Language') }}:</label>
                <select id="language" name="language">
                    <option value="en">English</option>
                    <option value="ru">Русский</option>
                    <option value="zh">中文</option>
                </select>
                <small style="color: rgb(248, 108, 108);">{{ _('Requires restart to take effect') }}</small>
            </div>
            <div class="form-group">
                <label for="key">{{ _('Key') }}:</label>
                <input type="text" id="key" name="key">
            </div>
            <div class="form-group">
                <label for="port">{{ _('Port') }}:</label>
                <input type="number" id="port" name="port" min="1024" max="65535">
                <small style="color: rgb(248, 108, 108);">{{ _('Requires restart to take effect') }}</small>
            </div>
            <div class="form-group">
                <label for="savePath">{{ _('Save Path') }}:</label>
                <input type="text" id="savePath" name="savePath">
            </div>
            <div class="form-group">
                <label>{{ _('Basic Notification Style') }}:</label>
                <label class="switch">
                    <input type="checkbox" id="basicNotifier" name="basicNotifier">
                    <span class="slider round"></span>
                </label>
                <br/>
                <small style="color: rgb(248, 108, 108);">{{ _('Requires restart to take effect') }}</small>
            </div>
            <div class="form-group">
                <label>{{ _('Show Icon') }}:</label>
                <label class="switch">
                    <input type="checkbox" id="showIcon" name="showIcon">
                    <span class="slider round"></span>
                </label>
                <br/>
                <small style="color: rgb(248, 108, 108);">{{ _('Requires restart to take effect, enable in _internal/config.ini if disabled') }}</small>
            </div>
            <button type="button" onclick="saveSettings()">{{ _('Save') }}</button>
        </form>

        <script>
            window.addEventListener('load', getSettings());
            
            function getSettings() {
                fetch('/settings/configs')
                  .then(response => response.json())
                  .then(data => {
                        if (data.success) {
                            const config = data.data;
                            document.getElementById('key').value = config.key;
                            document.getElementById('port').value = config.port;
                            document.getElementById('savePath').value = config.save_path;
                            document.getElementById('basicNotifier').checked = config.basic_notifier;
                            document.getElementById('showIcon').checked = config.show_icon;
                            document.getElementById('language').value = config.language || 'en';
                        }
                    })
                  .catch(error => {
                        console.error('{{ _("Error getting configuration") }}:', error);
                        alert('{{ _("Error getting configuration") }}');
                    });
            }

            function saveSettings() {
                const key = document.getElementById('key').value;
                const port = document.getElementById('port').value;
                const savePath = document.getElementById('savePath').value;
                const basicNotifier = document.getElementById('basicNotifier').checked;
                const showIcon = document.getElementById('showIcon').checked;
                const language = document.getElementById('language').value;
                
                if (!key || !port || !savePath) {
                    alert('{{ _("Please fill in all settings") }}');
                    return;
                }

                const settingsData = {
                    key: key,
                    port: port,
                    save_path: savePath,
                    basic_notifier: basicNotifier,
                    show_icon: showIcon,
                    language: language
                };

                fetch('/settings/configs', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(settingsData)
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('{{ _("Network response error") }}');
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.success) {
                        alert('{{ _("Settings saved!") }}');
                    } else {
                        console.log('{{ _("Failed to save settings") }}: ' + data.msg);
                    }
                })
                .catch(error => {
                    console.error('{{ _("Error saving settings") }}');
                    console.log('{{ _("Error saving settings") }}');
                });
                location.reload();
            }
        </script>
    </div>
</body>
</html>